import { defineStore } from 'pinia';
import userinfoApi from '@/api/modules/userinfo';
import playGoldApi from '@/api/modules/playGold';
import walletApi from '@/api/modules/wallet';

const userinfo = defineStore({
  id: 'userinfo',
  state: () => {
    return {
      userinfo: {},
      haslogged: false,
      wallet: <any>[],
      currentWallet: <any>{},
      digitalWallet: <any>[], // 数字钱包
      noticeUnreadNumber: 0, // 未读数量
      emailUnreadNumber: 0, // 未读数量
      notice: <any>[],
      email: <any>[],
      sportsInfo: {}, // sports信息
    };
  },
  persist: true,
  // 等同于vuex的getter
  getters: {
    getUserinfo: (state) => state.userinfo,
    getHaslogged: (state) => state.haslogged,
    getWallet: (state) => state.wallet,
    getCurrentWallet: (state) => state.currentWallet,
    getDigitalWallet: (state) => state.digitalWallet,
    getNoticeUnread: (state) => state.noticeUnreadNumber,
    getEmailUnread: (state) => state.emailUnreadNumber,
    getNotice: (state) => state.notice,
    getEmail: (state) => state.email,
    getSportsInfo: (state) => state.sportsInfo,
  },
  // pinia 放弃了 mutations 只使用 actions
  actions: {
    // actions可以用async做成异步形式
    setUserinfo(userinfo: any) {
      this.userinfo = userinfo;
    },
    // 设置是否登录
    setHaslogged(haslogged: boolean) {
      this.haslogged = haslogged;
    },
    // 设置钱包
    setWallet(wallet: any[]) {
      this.wallet = wallet;
      // 设置钱包后默认再选一次当前币种
      const findedWallet = this.wallet.find((im:any)=> im.currencyId === this.getCurrentWallet.currencyId);
      if(findedWallet){
        this.setCurrentWallet(findedWallet);
      }else{
        this.setCurrentWallet(this.wallet[0] || []);
      }
    },
    // 设置当前钱包显示
    setCurrentWallet(currentWallet: any) {
      this.currentWallet = currentWallet;
    },
    // 设置数字钱包
    setDigitalWallet(currencyId: string) {
      walletApi.getList({
        currencyId: currencyId || this.currentWallet.currencyId
      }).then((response:any)=>{
        if(response.code === 200){
          this.digitalWallet = response.rows;
        }
      });
    },
    // 设置sports信息
    setSportsInfo(sportsInfo: any) {
      this.sportsInfo = sportsInfo;
    },
    queryNoticeAndEmail(){
      userinfoApi.userEmailList({}).then((response:any)=>{
        if(response.code === 200){
          console.log("消息", response.rows.filter((im:any)=>im.readStatus === 0).length);
          this.email = response.rows || [];
          this.emailUnreadNumber = response.rows.filter((im:any)=>im.readStatus === 0).length;
        }
      });
      userinfoApi.webNoticeList({}).then((response:any)=>{
        if(response.code === 200){
          console.log("消息", response.rows.filter((im:any)=>im.readStatus === 0).length);
          this.notice = response.rows || [];
          this.noticeUnreadNumber = response.rows.filter((im:any)=>im.readStatus === 0).length;
        }
      });
    },
    // 获取皮夹子
    queryWallet(){
      // 请求皮夹子
      playGoldApi.userWallet({}).then((res:any)=>{
        this.setWallet(res.rows);
      });
    },
    // 获取钱包货币的icon
    queryCurrencyInfo(id: number | string, type: string = 'icon'){
      const currentWallet = this.wallet.find((item:any)=> item.currencyId == id) || {};
      console.log(type);
      if(type === 'icon'){
        return currentWallet.currencyIcon || "";
      }else if(type === 'name'){
        return currentWallet.currencyName || "";
      }
    },
    // 退出登录
    logout(){
      this.setHaslogged(false);
      this.setUserinfo({});
      this.setWallet([]);
      this.setCurrentWallet([]);
      this.setSportsInfo({});
      window.location.href = '/';
      localStorage.removeItem("enc");
    }
  },
});

export default userinfo;
